const product = require('../../utils/product.js')

Page({
    data: {// 页面的初始数据
        list: [],
        content: []
    },
    bindShowProductdetail: function (e) {
        const id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: `../detail/detail?id=${e.currentTarget.dataset.id}`
        })
    },
    onLoad: function () {
        var that = this  
        product.getSlides(function (result) {
            //取一级目录数据
            var data = result.data;
            const index = [];
            const index1 = [];
            for (var i = 0; i < data.length; i++) {
                if (data[i].parent_id == 0) {
                    index1[i] = data[i];
                }
            }
            //定义下标从零开始的数组
            var num = 0;
            for (var x in index1) {
                index[num] = index1[x];
                num += 1;
            }
            //取二级目录的数据
            var item = [];
            for (i = 0; i < num; i++) {
                for (var j = 0; j < data.length; j++) {
                    if (data[j].parent_id == index[i].id) {
                        item[j] = data[j];
                    }
                }
                var n = 0;
                var sub = [];
                for (var x in item) {
                    sub[n] = item[x];
                    n += 1;
                }
                //截取返回二级数据的前三个数据
                if (sub.length > 2) {
                    index[i]['item'] = sub.slice(0, 3);
                    item = [];
                } else {
                    index[i]['item'] = sub;
                    item = [];
                }
            }
            var ll = item.length;
            that.setData({
                list: index
            });
        })
    },
})

